/*
 * RendererLibroBusquedaSimple.java
 *
 * Created on 19 de diciembre de 2007, 20:54
 */
package com.codeko.apps.campanilla.ignotus;

import com.codeko.apps.campanilla.ignotus.util.Util;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.TitledBorder;

/**
 *
 * @author  campanilla
 */
public class RendererLibroBusquedaSimple extends javax.swing.JPanel implements ListCellRenderer {

    /** Creates new form RendererLibroBusquedaSimple */
    public RendererLibroBusquedaSimple() {
        initComponents();
    }

    private void mostrarFoto(ImageIcon img) {
        if (img != null) {
            Dimension max = new Dimension(80, 100);
            Dimension real = new Dimension(img.getIconWidth(), img.getIconHeight());
            Dimension escalada = Util.getTamanoEscalado(real, max);
            img = new ImageIcon(img.getImage().getScaledInstance((int) escalada.getWidth(), (int) escalada.getHeight(), Image.SCALE_SMOOTH));
        }
        lFoto.setIcon(img);
    }

    public void rellenar(Libro l) {
        ((TitledBorder) getBorder()).setTitle(l.getDatoString("nombre"));
        lTitulo.setText(l.getDatoString("titulo"));
        lAutor.setText(l.getDatoString("autor"));
        lTags.setText(l.getDatoString("tags"));
        lIsbn.setText(l.getDatoString("isbn"));
        if (l.getPrestamo().estaPrestado()) {
            lPrestado.setVisible(true);
            lPrestado.setText(l.getPrestamo().getPrestadoA());
        } else {
            lPrestado.setVisible(false);
        }
        lExtra.setText(l.getDatoString("edicion")+" "+l.getDatoString("publicacion"));
        mostrarFoto((ImageIcon) l.getDato("foto"));
    }

    @Override
    public Component getListCellRendererComponent(JList lista, Object libro, int indice, boolean isSelected, boolean cellHasFocus) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Libro l = null;
        if (libro instanceof Libro) {
            l = (Libro) libro;
            rellenar(l);
        } else {
            return null;
        }

        if (isSelected) {
            if (l.getPrestamo().estaPrestado()) {
                setBackground(Color.decode("#ff5555"));
            } else {
                setBackground(Color.ORANGE);
            }
        } else {
            if (l.getPrestamo().estaPrestado()) {
                setBackground(Color.decode("#ffaaaa"));
            } else {
                setBackground(Color.decode("#ffffff"));
            }
        }
        return this;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lTitulo = new javax.swing.JLabel();
        lAutor = new javax.swing.JLabel();
        lTags = new javax.swing.JLabel();
        lFoto = new javax.swing.JLabel();
        lIsbn = new javax.swing.JLabel();
        lPrestado = new javax.swing.JLabel();
        lExtra = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.codeko.apps.campanilla.ignotus.IgnotusApp.class).getContext().getResourceMap(RendererLibroBusquedaSimple.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        lTitulo.setIcon(resourceMap.getIcon("lineaBusquedaSimple.Titulo.etiqueta.icon")); // NOI18N
        lTitulo.setText(resourceMap.getString("lineaBusquedaSimple.Titulo.etiqueta.text")); // NOI18N
        lTitulo.setName("lineaBusquedaSimple.Titulo.etiqueta"); // NOI18N

        lAutor.setIcon(resourceMap.getIcon("lineaBusquedaSimple.Autor.etiqueta.icon")); // NOI18N
        lAutor.setText(resourceMap.getString("lineaBusquedaSimple.Autor.etiqueta.text")); // NOI18N
        lAutor.setName("lineaBusquedaSimple.Autor.etiqueta"); // NOI18N

        lTags.setIcon(resourceMap.getIcon("lineaBusquedaSimple.Tags.etiqueta.icon")); // NOI18N
        lTags.setText(resourceMap.getString("lineaBusquedaSimple.Tags.etiqueta.text")); // NOI18N
        lTags.setName("lineaBusquedaSimple.Tags.etiqueta"); // NOI18N

        lFoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lFoto.setText(resourceMap.getString("lFoto.text")); // NOI18N
        lFoto.setName("lFoto"); // NOI18N

        lIsbn.setIcon(resourceMap.getIcon("lIsbn.icon")); // NOI18N
        lIsbn.setText(resourceMap.getString("lIsbn.text")); // NOI18N
        lIsbn.setName("lIsbn"); // NOI18N

        lPrestado.setIcon(resourceMap.getIcon("lPrestado.icon")); // NOI18N
        lPrestado.setText(resourceMap.getString("lPrestado.text")); // NOI18N
        lPrestado.setName("lPrestado"); // NOI18N

        lExtra.setText(resourceMap.getString("lExtra.text")); // NOI18N
        lExtra.setName("lExtra"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lFoto, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lTags, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lExtra, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lAutor, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lIsbn, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                        .addComponent(lPrestado)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(lTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lAutor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lExtra)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lTags)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lIsbn)
                    .addComponent(lPrestado)))
            .addComponent(lFoto, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lAutor;
    private javax.swing.JLabel lExtra;
    private javax.swing.JLabel lFoto;
    private javax.swing.JLabel lIsbn;
    private javax.swing.JLabel lPrestado;
    private javax.swing.JLabel lTags;
    private javax.swing.JLabel lTitulo;
    // End of variables declaration//GEN-END:variables
}
